
*-------------------------------------------------------------------------------
*
* Create an output file to plot the relationship between AKM
* establishment fixed effects and establishment utility values estimated as in 
* Sorkin (QJE 2018)
* We estimate rents and cd using this relationship.
* Combined output file is: "$results/firm_effects_ver$ver"
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
*Step 1: Input the value data
*-------------------------------------------------------------------------------

import delimited using "$matlab\data_new\exp_V_skill_ver$ver.csv", clear
gen double betnr =v1
gen double exp_v =v2
drop v1 v2

gen double V = ln(exp_v)
drop exp_v
scalar V_n = V[1] //value of non-employment
gen V_adj = V-V_n
save "$temp/values_new_skill_ver$ver.dta", replace

use "$temp/workers_currentid_year_skill_ver$ver.dta", clear
gen workers_m = workers
collapse (sum) workers (mean) workers_m, by(currentid)
rename  currentid betnr
compress
save "$temp/workers_betnr_skill.dta", replace

use "$temp/values_new_skill_ver$ver", clear
merge 1:1 betnr using "$temp/workers_betnr_skill.dta"
drop if _merge == 2
drop _merge

erase "$temp/workers_betnr_skill.dta" // new

*Compute the V-psi relationship
merge 1:1 betnr using $temp/AKM_wages_skill_ver$ver.dta, keepusing(firm_fe)
keep if _merge==3 //we lose cases here because not all person spells are in the connected set
drop _merge


*-------------------------------------------------------------------------------
*Determine the psi-V slope in the microdata
*-------------------------------------------------------------------------------

keep betnr V V_adj firm_fe

*Keep relevant obs for the graph and regression
drop if missing(firm_fe) //drop if no estimated FE
rename V_adj value

*Studentize the regressors
egen std_fe = std(firm_fe)
egen std_V  = std(value)


*****************************


cap drop mean sd p 
cap drop value_p bin_value
gen mean = .
gen sd = . 
gen p = _n
replace p = . if p > 100
gen value_p = .
gen bin_value = .
qui {
forv p = 0(1)98 {
	local pp = `p' + 1
	dis as text "`p' to `pp'"
	centile std_V, centile(`p' `pp')
	replace value_p = `pp' if std_V >= r(c_1) & std_V < r(c_2)
	su std_fe if std_V >= r(c_1) & std_V < r(c_2), de
	
	replace mean = r(mean) in `pp'
	replace sd   = r(sd)/sqrt(r(N)) in `pp'
	
	centile std_V, centile(`p' `pp')
	su std_V if std_V >= r(c_1) & std_V < r(c_2)
	replace bin_value = r(mean) in `pp'
 }
} 
dis as text "95 to 100"
local p  = 99
local pp = 100 
centile std_V, centile(`p' `pp')
replace value_p = `pp' if std_V >= r(c_1) & std_V <= r(c_2)
su std_fe if std_V >= r(c_1) & std_V <= r(c_2), de
replace mean = r(mean) in `pp'
replace sd = r(sd)/sqrt(r(N)) in `pp' 
centile std_V, centile(`p' `pp')
su std_V if std_V >= r(c_1) & std_V <= r(c_2)
replace bin_value = r(mean) in `pp'
	
drop if value_p <=   2                        
drop if value_p >= 99 | missing(value_p)      

graph twoway (scatter mean bin_value if inrange(p, 2,99), mfcolor(none) ) (lfit mean bin_value if inrange(p, 2,99)),ytitle(Employer effects in earnings) xtitle(Value)     
drop mean sd p bin_value



*****************************


*Run psi-V regression

*Save slope for the sector based aggregate output
reg std_fe std_V, r
matrix b = e(b)
global beta = b[1,1]

twoway lfit std_fe std_V || lpoly std_fe std_V || hist std_V
su std_V, de

*Save coefficients for main analyses
reg firm_fe value
matrix b = e(b)
predict cd, residuals
predict rents, xb

preserve
	clear
	svmat b
	rename b1 std_V
	rename b2 cons
	gen method = "All OLS (trimmed)"
	order method std_V cons
	save $temp/psiV_slopes_skill_ver$ver, replace
restore	

corr std_fe std_V
su std_fe, de 
su std_V, de

preserve
label var value "value, centered"
label var cd "compensating differential"
label var rents "rents"
save $temp/firmfe_value_new_skill_ver$ver, replace
restore

cap log close





